package com.qfedu.mes2admin.sys.service.impl;

import com.qfedu.entity.sys.User;
import com.qfedu.exception.CustomerException;
import com.qfedu.mes2admin.sys.dao.UserDAO;
import com.qfedu.mes2admin.sys.params.LoginParam;
import com.qfedu.mes2admin.sys.service.LoginService;
import com.qfedu.utils.JwtUtils;
import com.qfedu.utils.MD5Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private UserDAO userDAO;

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    /**
     * 登录
     * @param param
     * @return
     */
    @Override
    public String login(LoginParam param) {
        if (param.getUsername().length()==0){
            throw new CustomerException("请输入账户密码！");
        }

        List<User> users = userDAO.userLogin(param);
        System.out.println(users);

        if (users.size()==0){
            throw new CustomerException("账号不存在！");
        }

        for (User user : users) {
            //对传入的密码进行md5加密后,与数据库加密密码比较
            String password = MD5Utils.MD5Lower(param.getPassword());

            if (user.getPassword().equals(password)){
                //String token = UUID.randomUUID().toString();

                //生成token,使用jwt后,理论上就不需要redis了
                Map<String,Object> map = new HashMap<>();
                map.put("uid",user.getId());
                String jwt = JwtUtils.createJwt(map);

                return jwt;

               // return token;
            }
        }
        throw new CustomerException("密码错误！");
    }

}
